#include"mutex.hpp"



using namespace mutex_module;

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;


int capacity = 1000;

//Mutex mylock;

void* routine(void* input)
{
    const char* ch = static_cast<const char*>(input);  //static_cast是c++关键字不在std::里
    while(1)
    {
        pthread_mutex_t lock;
        pthread_mutex_init(&lock,nullptr);
        pthread_mutex_lock(&lock);
        //LockGuard guard(mylock); //类
        //pthread_mutex_lock(&lock);
        if(capacity >= 1)
        {
            usleep(1000);
            std::cout << ch << "抢票:" << capacity << std::endl;
            capacity--;
            pthread_mutex_unlock(&lock);
            //pthread_mutex_unlock(&lock);
        }
        else
        {
            pthread_mutex_unlock(&lock);
            //pthread_mutex_unlock(&lock);
            break;
        }
    }
    return nullptr;
}


int main()
{
    Mutex mylock;
    
    pthread_t tid1 = 0;
    pthread_t tid2 = 0;
    pthread_t tid3 = 0;
    pthread_t tid4 = 0;

    

    pthread_create(&tid1,nullptr,routine,(void*)"线程一:");
    pthread_create(&tid2,nullptr,routine,(void*)"线程二:");
    pthread_create(&tid3,nullptr,routine,(void*)"线程三:");
    pthread_create(&tid4,nullptr,routine,(void*)"线程四:");

    pthread_join(tid1,nullptr);
    pthread_join(tid2,nullptr);
    pthread_join(tid3,nullptr);
    pthread_join(tid4,nullptr);

    return 0;
}